﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ShareContent;

namespace AncientEmpires_025_132
{
    public class Menu : VisibleGameEntity
    {
        //Số lượng các thành phần trong 1 menu
        private int _nSubMenu;
        public int NSubMenu
        {
            get { return _nSubMenu; }
            set { _nSubMenu = value; }
        }
        
        //Menu được active
        private bool _isActive;
        public bool IsActive
        {
            get { return _isActive; }
            set { _isActive = value; }
        }

        Sprite2D _background;

        public Sprite2D Background
        {
            get { return _background; }
            set { _background = value; }
        }

        private Vector2 _size;

        public Vector2 Size
        {
            get { return _size; }
            set { _size = value; }
        }

        private Vector2 _position;

        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public MyButton LoadItem(ContentManager content, SliceMenu slice, String strFoler, Vector2 parentPosition)
        {
            Sprite2D tempSprite2DNormal = null;
            Sprite2D tempSprite2DHover = null;
            Sprite2D tempSprite2DPressed = null;
            MyButton tempMenuOption = null;

            Texture2D[] textureNormal;
            Texture2D[] textureHover;
            Texture2D[] texturePressed;
            Texture2D[] tempTexture;

            tempTexture = new Texture2D[1];
            tempTexture[0] = content.Load<Texture2D>(strFoler + slice.strTexture + "_hover");
            textureHover = tempTexture;

            tempTexture = new Texture2D[1];
            tempTexture[0] = content.Load<Texture2D>(strFoler + slice.strTexture + "_normal");
            textureNormal = tempTexture;

            tempTexture = new Texture2D[1];
            tempTexture[0] = content.Load<Texture2D>(strFoler + slice.strTexture + "_pressed");
            texturePressed = tempTexture;

            tempSprite2DNormal = new Sprite2D(textureNormal, new Vector2(0, 0));
            tempSprite2DNormal.Position = new Vector2(slice.positionScale.X * Size.X + parentPosition.X, slice.positionScale.Y * Size.Y + parentPosition.Y);
            tempSprite2DNormal.Size = new Vector2(slice.sizeScale.X * Size.X, slice.sizeScale.Y * Size.Y);
            tempSprite2DNormal.NTextures = 1;

            tempSprite2DHover = new Sprite2D(textureHover, new Vector2(0, 0));
            tempSprite2DHover.Position = new Vector2(slice.positionScale.X * Size.X + parentPosition.X, slice.positionScale.Y * Size.Y + parentPosition.Y);
            tempSprite2DHover.Size = new Vector2(slice.sizeScale.X * Size.X, slice.sizeScale.Y * Size.Y);
            tempSprite2DHover.NTextures = 1;

            tempSprite2DPressed = new Sprite2D(texturePressed, new Vector2(0, 0));
            tempSprite2DPressed.Position = new Vector2(slice.positionScale.X * Size.X + parentPosition.X, slice.positionScale.Y * Size.Y + parentPosition.Y);
            tempSprite2DPressed.Size = new Vector2(slice.sizeScale.X * Size.X, slice.sizeScale.Y * Size.Y);
            tempSprite2DPressed.NTextures = 1;

            tempMenuOption = new MyButton(content, tempSprite2DNormal, tempSprite2DHover, tempSprite2DPressed);

            return tempMenuOption;
        }

        // Load List button from list slice
        public List<MyButton> LoadListItem(ContentManager content, List<SliceMenu> listSlice, String strFoler, Vector2 parentPosition)
        {
            List<MyButton> listResult = new List<MyButton>();
            List<Texture2D[]> listTextureNormal = new List<Texture2D[]>();
            List<Texture2D[]> listTextureHover = new List<Texture2D[]>();
            List<Texture2D[]> listTexturePressed = new List<Texture2D[]>();
            Texture2D[] tempTexture;
            for (int i = 0; i < listSlice.Count; i++)
            {
                tempTexture = new Texture2D[1];
                tempTexture[0] = content.Load<Texture2D>(strFoler + listSlice[i].strTexture + "_hover");
                listTextureHover.Add(tempTexture);

                tempTexture = new Texture2D[1];
                tempTexture[0] = content.Load<Texture2D>(strFoler + listSlice[i].strTexture + "_normal");
                listTextureNormal.Add(tempTexture);

                tempTexture = new Texture2D[1];
                tempTexture[0] = content.Load<Texture2D>(strFoler + listSlice[i].strTexture + "_pressed");
                listTexturePressed.Add(tempTexture);
            }

            Sprite2D tempSprite2DNormal = null;
            Sprite2D tempSprite2DHover = null;
            Sprite2D tempSprite2DPressed = null;
            MyButton tempMenuOption = null;
            for (int i = 0; i < listTextureNormal.Count; i++)
            {
                tempSprite2DNormal = new Sprite2D(listTextureNormal[i], new Vector2(0, 0));
                tempSprite2DNormal.NormalDelay = 8 * i + 8;
                tempSprite2DNormal.Position = new Vector2(listSlice[i].positionScale.X * Size.X + parentPosition.X, listSlice[i].positionScale.Y * Size.Y + parentPosition.Y);
                tempSprite2DNormal.Size = new Vector2(listSlice[i].sizeScale.X * Size.X, listSlice[i].sizeScale.Y * Size.Y);
                tempSprite2DNormal.NTextures = 1;

                tempSprite2DHover = new Sprite2D(listTextureHover[i], new Vector2(0, 0));
                tempSprite2DHover.NormalDelay = 8 * i + 8;
                tempSprite2DHover.Position = new Vector2(listSlice[i].positionScale.X * Size.X + parentPosition.X, listSlice[i].positionScale.Y * Size.Y + parentPosition.Y);
                tempSprite2DHover.Size = new Vector2(listSlice[i].sizeScale.X * Size.X, listSlice[i].sizeScale.Y * Size.Y);
                tempSprite2DHover.NTextures = 1;

                tempSprite2DPressed = new Sprite2D(listTexturePressed[i], new Vector2(0, 0));
                tempSprite2DPressed.NormalDelay = 8 * i + 8;
                tempSprite2DPressed.Position = new Vector2(listSlice[i].positionScale.X * Size.X + parentPosition.X, listSlice[i].positionScale.Y * Size.Y + parentPosition.Y);
                tempSprite2DPressed.Size = new Vector2(listSlice[i].sizeScale.X * Size.X, listSlice[i].sizeScale.Y * Size.Y);
                tempSprite2DPressed.NTextures = 1;

                tempMenuOption = new MyButton(content, tempSprite2DNormal, tempSprite2DHover, tempSprite2DPressed);
                listResult.Add(tempMenuOption);
            }
            return listResult;
        }

        // Load List static item from list slice
        public List<Sprite2D> LoadStaticListItem(ContentManager content, List<SliceMenu> listSlice, String strFolder)
        {
            List<Sprite2D> listResult = new List<Sprite2D>();
            Texture2D[] tempTexture;
            for (int i = 0; i < listSlice.Count; i++)
            {
                tempTexture = new Texture2D[1];
                tempTexture[0] = content.Load<Texture2D>(strFolder + listSlice[i].strTexture);
                Vector2 spritePosition = new Vector2(listSlice[i].positionScale.X * Size.X, listSlice[i].positionScale.Y * Size.Y);
                Vector2 spriteSize = new Vector2(listSlice[i].sizeScale.X * Size.X, listSlice[i].sizeScale.Y * Size.Y);
                Sprite2D tempSprite = new Sprite2D(tempTexture, spritePosition, spriteSize);
                tempSprite.Size = spriteSize;
                listResult.Add(tempSprite);
            }
            return listResult;
        }

        public Sprite2D LoadStaticItem(ContentManager content,String strTexture, Vector2 sizeScale, Vector2 positionScale, String strFolder, Vector2 parentPosition)
        {
            Sprite2D result;
            Texture2D[] tempTexture;
            tempTexture = new Texture2D[1];
            tempTexture[0] = content.Load<Texture2D>(strFolder+strTexture);
            result = new Sprite2D(tempTexture, new Vector2(parentPosition.X + positionScale.X * Size.X, parentPosition.Y + positionScale.Y * Size.Y));
            result.Size = new Vector2(sizeScale.X * Size.X, sizeScale.Y * Size.Y);
            return result;
        }
    }
}
